/**
 * =================================================
 *
 * @copyright 
 * =================================================
 */
package com.lemon.utils.wx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串处理相关工具类
 *
 * @author qfq
 * @date 2012-10-16 15:37:14
 * @version 1.0
 */
public abstract class StringUtil {
	private static Logger log = LoggerFactory.getLogger(StringUtil.class);



	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		return s.replace("-","");
	}

	
	/**
	 * 判断字符串是否为空或null，为空返回true
	 *
	 * @param str
	 *            待判定字符串
	 * @return 是否为空或null
	 */
	public static boolean isEmpty(String str) {
		return (str == null || "".equals(str.trim()));
	}
	
	/**
	 * 判断字符串是非空，不为空返回true
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static boolean isEmpty(String... strs) {
		for (String s : strs) {
			return isEmpty(s);
		}
		return false;
	}

	/**
	 * 判断参数是否为空，只有其中有一个参数为空则返回true，否则返回false
	 * 
	 * @author:zzy
	 * @date 2013-11-26上午10:42:34
	 * @modifier:
	 * @modify_description:
	 * @param objects
	 * @return
	 * @version v1.0.0
	 */
	public static boolean isEmpty(Object... objects) {
		boolean result = false;
		for (Object object : objects) {
			if (object instanceof String) {
				if (object == null || "".equals(object)) {
					return true;
				}
			}
			if (object instanceof Integer) {
				if (object == null || 0 == (Integer) object) {
					return true;
				}
			}
			if (object instanceof Double) {
				if (object == null || 0 == (Double) object) {
					return true;
				}
			}
			if (object instanceof Float) {
				if (object == null || 0 == (Float) object) {
					return true;
				}
			}
			if (object instanceof Long) {
				if (object == null || 0 == (Long) object) {
					return true;
				}
			}
			if (object instanceof Date) {
				if (object == null || null == (Date) object) {
					return true;
				}
			}
			if (object instanceof Timestamp) {
				if (object == null || null == (Timestamp) object) {
					return true;
				}
			}
			if (object instanceof Collection) {
				if (object == null || 0 == ((Collection) object).size()) {
					return true;
				}
			}
		
			if (object == null) {
				return true;
			}
		}
		return result;
	}

	/**
	 * 对象转为string格式，为空时，转为默认带入字符串
	 *
	 * @param obj
	 *            待转对象
	 * @param defaultStr
	 *            默认字符串
	 * @return 字符串
	 */
	public static String objToString(Object obj, String defaultStr) {
		if (obj == null) {
			return defaultStr;
		} else {
			return obj.toString().trim();
		}
	}

	/**
	 * 判断字符串长度是否合法
	 *
	 * @param instr
	 *            输入字符串
	 * @param minLength
	 *            最小长度
	 * @param maxLength
	 *            最大长度
	 * @return true=合法
	 */
	public static boolean chackStringLength(String instr, int minLength, int maxLength) {
		return !(instr.length() < minLength || instr.length() > maxLength);
	}

	/**
	 * 判断字符串是否全是数字
	 *
	 * @param str
	 *            输入字符串
	 * @return true=全是数字
	 */
	public static boolean isNum(String str) {
		if (null == str) {
			return false;
		}
		return str.matches("\\d+");
	}

	/**
	 * 字符串长度处理，过长切除，不足补空格.
	 *
	 * @param str
	 *            传入字符串
	 * @param length
	 *            传入指定长度(字节数)
	 * @param defaultStr
	 *            默认字符
	 * @param isRight
	 *            是否右补齐
	 * @return 返回指定长度的字符串
	 */
	public static String supplyString(String str, int length, String defaultStr, boolean isRight) {
		if (str != null) {
			int len = str.length();
			if (len < length) {
				if (isRight) {
					for (int i = 0; i < (length - len); i++) {
						str = str + defaultStr;
					}
				} else {
					for (int i = 0; i < (length - len); i++) {
						str = defaultStr + str;
					}
				}
			} else {
				str = str.substring(0, length);
			}
		} else {// 空字符串
			if (isRight) {
				for (int i = 0; i < length; i++) {
					str = str + defaultStr;
				}
			} else {
				for (int i = 0; i < length; i++) {
					str = defaultStr + str;
				}
			}
		}
		return str;
	}

	/**
	 * 字符串倒转
	 *
	 * @param str
	 *            输入字符串
	 * @return 倒转后字符串
	 */
	public static String reverseStr(String str) {
		StringBuilder strbuff = new StringBuilder();
		for (int i = str.length() - 1; i >= 0; i--) {
			strbuff.append(str.charAt(i));
		}
		return strbuff.toString();
	}

	/**
	 * 生成随机几位数字
	 *
	 * @param k
	 *            生成位数
	 * @return
	 */
	public static String createRand(int k) {
		Random random = new Random();
		String sRand = "";
		for (int i = 0; i < k; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
		}
		return sRand;
	}



	public static String DateToString(java.util.Date date, String format) {
		String result = null;
		if(isEmpty(format)){
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		if (date != null) {
			result = formatter.format(date);
		} else {
			result = "";
		}
		return result;
	}
	


	public static Integer getInteger(String str) {
		Integer intTemp = 0;
		try {
			intTemp = Integer.valueOf(str);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return intTemp;
	}



	/**
	 * 把map类型参数拼装成url参数连接的字符串格式
	 * 
	 * @author:zzy
	 * @date 2015-4-21上午11:06:13
	 * @modifier:
	 * @date:
	 * @modify_description:
	 * @param params
	 *            待拼接的参数map
	 * @return 拼装成url参数连接的字符串
	 * @version v1.0.0
	 */
	public static String getParamString(Map<String, String> params) {
		StringBuffer result = new StringBuffer("");
		if (params == null || params.isEmpty()) {
			return "";
		}
		Set<Map.Entry<String, String>> set = params.entrySet();
		Iterator<Map.Entry<String, String>> it = set.iterator();
		if (it.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
			String param = entry.getKey() + "=" + entry.getValue();
			result.append(param);
		}
		while (it.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
			String param = entry.getKey() + "=" + entry.getValue();
			result.append("&" + param);
		}
		return result.toString();
	}
	/**
	 * 生成订单号
	 */
	public static String createOrderNo(String userId){
		String userId_str = supplyString(userId, 7, "0", false);
		String radom = createRand(4);
		String time_str = DateToString(new Date(), "yyyyMMddHHmmss");
		String ordroNo = time_str + radom + userId_str;
		return ordroNo;
	}
	
	
	public  static String filter(String str) {  
       
		if(str.trim().isEmpty()){  
			return str;  
	    }  
        String pattern="[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";  
        String reStr="";  
        Pattern emoji=Pattern.compile(pattern);  
        Matcher  emojiMatcher=emoji.matcher(str);  
        str=emojiMatcher.replaceAll(reStr).replaceAll("[\\ue000-\\uefff]", "");  
        return str;  
	}
	
	public static String encodeEmoji(String orgStr) { 
		if (isEmpty(orgStr)) { 
			return orgStr; 
		} 
		String temp = orgStr; 
		Pattern pattern = Pattern.compile("[^\u0000-\uffff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE); 
		Matcher matcher = pattern.matcher(orgStr); 
		while (matcher.find()) { 
			StringBuilder sb = new StringBuilder("[em:"); 
			String mStr = matcher.group(); 
			for (int i = 0; i < mStr.length(); i++) { 
				int emoji = mStr.charAt(i); 
				if (i < mStr.length() - 1) { 
					sb.append(emoji).append('-'); 
				} else { 
					sb.append(emoji).append(']'); 
				} 
			} 
			temp = temp.replaceAll(mStr, sb.toString()); 
		} 
		return temp; 
	} 
	public static String decodeEmoji(String orgStr) { 
		if (isEmpty(orgStr)) { 
			return orgStr; 
		} 
		String temp = orgStr; 
		Pattern pattern = Pattern.compile("\\[em:[\\d\\-]+\\]", Pattern.CASE_INSENSITIVE); 
		Matcher matcher = pattern.matcher(orgStr); 
		while (matcher.find()){ 
			StringBuilder sb = new StringBuilder("[em:"); 
			StringBuilder emojiSb = new StringBuilder(); 
			String mStr = matcher.group(); 
			String[] emojis = mStr.substring(4, mStr.length() - 1).split("-"); 
			for (int i = 0; i < emojis.length; i++) { 
				int emoji = Integer.parseInt(emojis[i]); 
				emojiSb.append((char)emoji); 
				if (i < emojis.length - 1) { 
					sb.append(emoji).append("-"); 
				} else { 
					sb.append(emoji).append("]"); 
				} 
			} 
			System.out.println(emojiSb);
			temp = temp.replace(sb.toString(), emojiSb.toString()); 
		} 
		return temp; 
	}

	

}
